package cn.lg.soar.boot.fc.controller;

import cn.lg.soar.boot.fc.entity.ProcessApply;
import cn.lg.soar.boot.fc.entity.TenancyRoom;
import cn.lg.soar.boot.fc.entity.vo.RepairVo;
import cn.lg.soar.boot.fc.entity.vo.RoomVo;
import cn.lg.soar.boot.fc.entity.vo.TenancyRoomVo;
import cn.lg.soar.boot.fc.service.ProcessApplyService;
import cn.lg.soar.boot.fc.service.RoomService;
import cn.lg.soar.common.data.msg.I18n;
import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.group.Create;
import cn.lg.soar.core.annotations.constraints.group.Update;
import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.mvc.enums.ControllerConst;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.mvc.annotations.Log;
import cn.lg.soar.system.auth.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.boot.fc.entity.Repair;
import cn.lg.soar.boot.fc.service.RepairService;

/**
 * 报修表
 * @author ZhuanZ（无密码）
 * @since 2025-01-22
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Api(tags = "报修表")
@Validated
@RestController
@RequestMapping("fc/repair")
public class RepairController extends BaseController<Repair, RepairService> {

    @Resource
    private RoomService roomService;
    @Autowired
    protected ProcessApplyService processApplyService;

    @Override
    protected Serializable getId(Repair entity) {
        return entity.getId();
    }

    @Log(value = "新增报修表", data = "#p0", dataId = "#$r.data")
    @IdempotentByParam("#p0")
    @ApiOperation(value = ControllerConst.CREATE_NAME)
    @RequestMapping(path = "create", method = RequestMethod.POST)
    public HttpResult<Serializable> create(@RequestBody @Validated(Create.class) Repair data) {
        return super.createResult(data);
    }

    @Log(value = "修改报修表", data = "#p0", dataId = "#p0.id")
    @ApiOperation(value = ControllerConst.UPDATE_NAME)
    @RequestMapping(path = "update", method = RequestMethod.POST)
    public HttpResult<Void> update(@RequestBody @Validated(Update.class) Repair data) {
        return super.updateResult(data);
    }

    @Log(value = "删除报修表", data = "#p0")
    @ApiOperation(value = ControllerConst.REMOVE_NAME, notes = "根据 id 删除")
    @RequestMapping(path = "remove", method = RequestMethod.POST)
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        return super.removeResult(ids);
    }

    @ApiOperation(value = ControllerConst.DETAIL_NAME, notes = "根据 ID 获取一条数据")
    @RequestMapping(path = "detail", method = RequestMethod.GET)
    public HttpResult<RepairVo> detail(@RequestParam Long id) {
        Repair repair = super.detailResult(id).getData();
        RepairVo repairVo = new RepairVo();
        BeanUtils.copyProperties(repair, repairVo);
        RoomVo roomVo = roomService.getVoData(repair.getRoomId());
        String roomInfo = "";
        if(roomVo != null) {
            roomInfo = roomVo.getCampusEasyVo().getName()+"*"+roomVo.getBuildingEasyVo().getName()+"*"+roomVo.getUnitEasyVo().getName()+"*"
                    +roomVo.getFloorEasyVo().getName()+"*"+roomVo.getRoomCode();
        }
        repairVo.setRoomInfo(roomInfo);
        return HttpResult.data(repairVo);
    }

    @ApiOperation(value = ControllerConst.QUERY_NAME, notes = "多功能查询（分页、列表、统计）")
    @RequestMapping(path = "query", method = RequestMethod.GET)
    public HttpResult<PageResult<RepairVo>> query(PageQuery pageQuery, QueryParams params){
        params.setFields(new ArrayList<>());
        PageResult<RepairVo> pageResult = new PageResult<>();
        List<RepairVo> list = new ArrayList<>();
        List<Repair> repairQuery = super.queryResult(pageQuery, params).getData().getList();
        for (Repair repair : repairQuery) {
            RepairVo repairVo = new RepairVo();
            BeanUtils.copyProperties(repair, repairVo);
            RoomVo roomVo = roomService.getVoData(repair.getRoomId());
            String roomInfo = "";
            if(roomVo != null) {
                roomInfo = roomVo.getCampusEasyVo().getName()+"*"+roomVo.getBuildingEasyVo().getName()+"*"+roomVo.getUnitEasyVo().getName()+"*"
                        +roomVo.getFloorEasyVo().getName()+"*"+roomVo.getRoomCode();
            }
            repairVo.setRoomInfo(roomInfo);
            list.add(repairVo);
        }
        pageResult.setList(list);
        return HttpResult.data(pageResult);
    }

    @Log(value = "导出报修表")
    @ApiOperation(value = ControllerConst.EXPORT_NAME)
    @RequestMapping(path = "export", method = RequestMethod.GET)
    public void exports(String filename, QueryParams params, HttpServletResponse resp){
        super.exportExcel(filename, params, resp);
    }

    @Log(value = "导入报修表")
    @ApiOperation(value = ControllerConst.IMPORT_NAME)
    @RequestMapping(path = "import", method = RequestMethod.POST)
    public HttpResult<Void> imports(HttpServletRequest req){
        return super.importExcel(req);
    }

    @Log(value = "维修")
    @PostMapping("/maintain")
    public HttpResult<Void> maintain(@RequestBody @Valid Repair repair) {
        return super.service.maintain(repair);
    }

    @Log(value = "维修完成")
    @ApiOperation(value = "维修完成")
    @PostMapping("/maintainComplete")
    public HttpResult<Void> maintainComplete(@RequestParam Long id) {
        return super.service.maintainComplete(id);
    }

    @ApiOperation(value = "维修记录", notes = "根据 房间ID 获取维修记录")
    @RequestMapping(path = "repairList", method = RequestMethod.GET)
    public HttpResult<PageResult<RepairVo>> repairList(PageQuery pageQuery, @RequestParam Long roomId) {
        QueryParams params = new QueryParams();
        params.setFields(new ArrayList<>());
        List<String> f = new ArrayList<>();
        f.add("roomId-eq~"+roomId);
        List<String> orderBy = new ArrayList<>();
        orderBy.add("id");
        params.setF(f);
        params.setOrderBy(orderBy);

        PageResult<RepairVo> pageResult = new PageResult<>();
        List<RepairVo> list = new ArrayList<>();
        List<Repair> repairQuery = super.queryResult(pageQuery, params).getData().getList();
        for (Repair repair : repairQuery) {
            RepairVo repairVo = new RepairVo();
            BeanUtils.copyProperties(repair, repairVo);
            RoomVo roomVo = roomService.getVoData(repair.getRoomId());
            String roomInfo = "";
            if(roomVo != null) {
                roomInfo = roomVo.getCampusEasyVo().getName()+"*"+roomVo.getBuildingEasyVo().getName()+"*"+roomVo.getUnitEasyVo().getName()+"*"
                        +roomVo.getFloorEasyVo().getName()+"*"+roomVo.getRoomCode();
            }
            repairVo.setRoomInfo(roomInfo);
            list.add(repairVo);
        }
        pageResult.setList(list);
        return HttpResult.data(pageResult);
    }

    @ApiOperation(value = ControllerConst.DETAIL_NAME, notes = "根据 ID 获取一条数据")
    @RequestMapping(path = "repairDetail", method = RequestMethod.GET)
    public HttpResult<RepairVo> repairDetail(@RequestParam Long processApplyId) {
        ProcessApply processApply = processApplyService.getById(processApplyId);
        if(processApply == null) {
            return HttpResult.fail("B0000", I18n.build("退房申请为空！"));
        }
        if(processApply.getRepairId() == null){
            return HttpResult.fail("B0000", I18n.build("报修信息为空！"));
        }
        Repair repair = super.service.getById(processApply.getRepairId());
        RepairVo repairVo = new RepairVo();
        BeanUtils.copyProperties(repair, repairVo);
        RoomVo roomVo = roomService.getVoData(repair.getRoomId());
        String roomInfo = "";
        if(roomVo != null) {
            roomInfo = roomVo.getCampusEasyVo().getName()+"*"+roomVo.getBuildingEasyVo().getName()+"*"+roomVo.getUnitEasyVo().getName()+"*"
                    +roomVo.getFloorEasyVo().getName()+"*"+roomVo.getRoomCode();
        }
        repairVo.setRoomInfo(roomInfo);
        return HttpResult.data(repairVo);
    }

    @ApiOperation(value = "用户报修记录")
    @RequestMapping(path = "repairRecord", method = RequestMethod.GET)
    public HttpResult<PageResult<RepairVo>> repairRecord(PageQuery pageQuery, @RequestParam Long userId) {
        QueryParams params = new QueryParams();
        params.setFields(new ArrayList<>());
        List<String> f = new ArrayList<>();
        f.add("userId-eq~"+userId);
        List<String> orderBy = new ArrayList<>();
        orderBy.add("id");
        params.setF(f);
        params.setOrderBy(orderBy);

        PageResult<RepairVo> pageResult = new PageResult<>();
        List<RepairVo> list = new ArrayList<>();
        List<Repair> repairQuery = super.queryResult(pageQuery, params).getData().getList();
        for (Repair repair : repairQuery) {
            RepairVo repairVo = new RepairVo();
            BeanUtils.copyProperties(repair, repairVo);
            RoomVo roomVo = roomService.getVoData(repair.getRoomId());
            String roomInfo = "";
            if(roomVo != null) {
                roomInfo = roomVo.getCampusEasyVo().getName()+"*"+roomVo.getBuildingEasyVo().getName()+"*"+roomVo.getUnitEasyVo().getName()+"*"
                        +roomVo.getFloorEasyVo().getName()+"*"+roomVo.getRoomCode();
            }
            repairVo.setRoomInfo(roomInfo);
            list.add(repairVo);
        }
        pageResult.setList(list);
        return HttpResult.data(pageResult);
    }

}